﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using Red.Controls.Utils;
using Red.Core.GoogleAnalytics;

namespace Red.Launcher
{
    public partial class RecentFiles
    {
        #region Private Data

        private readonly string _analyticsCategory = typeof( RecentFiles ).Name;

        #endregion Private Data

        #region C-Tor

        partial void InitializeAnalytics()
        {
            RecentFileActivated += Analytics_RecentFileActivated;

            Loaded += RecentFiles_Loaded;
        }

        #endregion C-Tor

        #region Event Handlers

        private void RecentFiles_Loaded( object sender, RoutedEventArgs e )
        {
            ScrollBar scrollbar = Helpers.FindFirstVisualChild<ScrollBar>( _listbox );
            scrollbar.ValueChanged += Scrollbar_ValueChanged;

            _filter.TextChanged += _filter_TextChanged;
        }

        private void _filter_TextChanged( object sender, TextChangedEventArgs e )
        {
            GoogleAnalyticsService.Instance.SendEvent( "Filter", _analyticsCategory, _filter.Text, (float)_listbox.Items.Count );
        }

        private void Scrollbar_ValueChanged( object sender, RoutedPropertyChangedEventArgs<double> e )
        {
            GoogleAnalyticsService.Instance.SendEvent( "Scroll", _analyticsCategory, (int)e.NewValue );
        }

        private void Analytics_RecentFileActivated( string file )
        {
            GoogleAnalyticsService.Instance.SendEvent( "Open", _analyticsCategory, file );
        }

        #endregion Event Handlers
    }
}
